package com.ljy.jsoup;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.net.URL;

public class jsoup {

    @Test
    public void testJsoupUrl() throws Exception{
        // 解析url地址
        Document document = Jsoup.parse(new URL("http://www.itcast.cn/"), 1000);
        // 获取title的内容
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
    }

    @Test
    public void testString() throws Exception {
        // 使用工具类读取文件，获取字符串
        String html = FileUtils.readFileToString(new File("C:\\Users\\AikeTech\\IdeaProjects\\WebCrawler" +
                "\\src\\main\\webapp\\01_test.html"), "UTF-8");
        // 解析字符串
        Document document = Jsoup.parse(html);
        // 获取title的内容
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
    }

    @Test
    public void testJsoupHtml() throws Exception {
        // 解析文件
        Document document = Jsoup.parse(new File("C:\\Users\\AikeTech\\IdeaProjects\\WebCrawler" +
                "\\src\\main\\webapp\\01_test.html"),"UTF-8");
        // 获取title的内容
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
    }

    @Test
    public void testDOM() throws Exception {
        // 解析文件
        Document document = Jsoup.parse(new File("C:\\Users\\AikeTech\\IdeaProjects\\WebCrawler" +
                "\\src\\main\\webapp\\01_test.html"),"UTF-8");
        // 1.根据id查询元素getElementById
        Element city_bj = document.getElementById("city_bj");
        System.out.println(city_bj.text());
        // 2.根据标签获取元素getElementsByTag
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
        // 3.根据class获取元素getElementsByClass
        Element s_name = document.getElementsByClass("s_name").last();
        System.out.println(s_name.text());
        Elements s_name1 = document.getElementsByClass("s_name");
        System.out.println("==============================================");
        for (Element element : s_name1){
            System.out.println(element.text());
        }
        System.out.println("==============================================");
        // 4.根据属性获取元素getElementsByAttribute
        Element abc = document.getElementsByAttribute("abc").first();
        System.out.println(abc.text());
        Element first = document.getElementsByAttributeValue("class", "s_name").first();
        System.out.println(first.text());

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
        Element element = document.getElementById("test");
        // 1.从元素中获取id
        String id = element.id();
        System.out.println("id:" + id);
        // 2.从元素中获取className
        String className = element.className();
        System.out.println("className:" + className);
        // 3.   从元素中获取属性的值attr
        String attr = element.attr("id");
        System.out.println("attr:" + attr);
        // 4. 从元素中获取所有属性attributes
        String string = element.attributes().toString();
        System.out.println("string:" + string);
        // 5. 从元素中获取文本内容text
        String text = element.text();
        System.out.println("text:" + text);
    }

    @Test
    public void testSelector() throws Exception {

        //解析html文件，获取Document对象
        Document doc = Jsoup.parse(new File("C:\\Users\\AikeTech\\IdeaProjects\\WebCrawler" +
                "\\src\\main\\webapp\\01_test.html"), "utf8");

        //tagname: 通过标签查找元素，比如：span
        Elements elements = doc.select("span");
        //for (Element element : elements) {
        //    System.out.println(element.text());
        //}

        //#id: 通过ID查找元素，比如：#city_bj
        //Element element = doc.select("#city_bj").first();

        //.class: 通过class名称查找元素，比如：.class_a
        //Element element = doc.select(".class_a").first();

        //[attribute]: 利用属性查找元素，比如：[abc]
        Element element = doc.select("[abc]").first();

        //[attr=value]: 利用属性值来查找元素，比如：[class=s_name]
        Elements elements1 = doc.select("[class=s_name]");
        for (Element element1 : elements1) {
            System.out.println(element1.text());
        }


        //打印结果
        System.out.println("获取到的结果是：" + element.text());
    }

    @Test
    public void testSelector2()throws Exception{
        //解析html文件，获取Document对象
        Document doc = Jsoup.parse(new File("C:\\Users\\tree\\Desktop\\test.html"), "utf8");

        //el#id: 元素+ID，比如： h3#city_bj
        Element element = doc.select("h3#city_bj").first();

        //el.class: 元素+class，比如： li.class_a
        element = doc.select("li.class_a").first();

        //el[attr]: 元素+属性名，比如： span[abc]
        element = doc.select("span[abc]").first();

        //任意组合: 比如：span[abc].s_name
        element = doc.select("span[abc].s_name").first();

        //ancestor child: 查找某个元素下子元素，比如：.city_con li 查找"city_con"下的所有li
        Elements elements = doc.select(".city_con li");

        //parent > child: 查找某个父元素下的直接子元素，比如：
        //.city_con > ul > li 查找city_con第一级（直接子元素）的ul，再找所有ul下的第一级li
        elements = doc.select(".city_con > ul > li");

        //parent > *: 查找某个父元素下所有直接子元素
        elements = doc.select(".city_con > ul > *");


        System.out.println("获取到的内容是："+element.text());

        for (Element element1 : elements) {
            System.out.println("遍历的结果："+element1.text());
        }
    }
}
